﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Entity;
using BAL;
using Common;
namespace Web
{
    public partial class PostQuesType : System.Web.UI.Page
    {
        private int editid = 0;
        private int delid = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!UserValidate.CheckLogin(this.Request, "MasterQuesType"))
            {
                Response.Write("<script type=\"text/javascript\">top.location.href='login.aspx';</script>");
                Response.Redirect("login.aspx");
                Response.End();
            }

            if (Request.QueryString["editid"] != null)
            {
                editid = Convert.ToInt32(Request.QueryString["editid"].Trim());
            }
            if (Request.QueryString["delid"] != null)
            {
                delid = Convert.ToInt32(Request.QueryString["delid"].Trim());
            }
            if (!IsPostBack)
            {
                if (editid > 0)
                {
                    QuesTypeInfo quesTypeInfo = QuesType.GetQuesTypeInfoById(editid);
                    QuesTypeName.Text = quesTypeInfo.Name.ToString();
                    QuesTypeKeyword.Text = quesTypeInfo.Keyword;
                }
                if (delid > 0)
                {
                    QuesType.DelQuesType(delid);
                    Response.Redirect("admininfo.aspx");
                    return;
                }
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (QuesTypeName.Text.Trim() != "" || QuesTypeKeyword.Text.Trim() != "")
            {
                QuesTypeInfo quesTypeInfo = new QuesTypeInfo();
                quesTypeInfo.Name = QuesTypeName.Text.Trim();
                quesTypeInfo.Keyword = QuesTypeKeyword.Text.Trim();
                if (editid > 0)
                {
                    if (QuesType.CheckQuesTypeExistByKeyword(QuesTypeKeyword.Text.Trim(), editid))
                    {
                        Utility.ShowJsAlert("关键字已存在", this);
                    }
                    else
                    {
                        quesTypeInfo.Id = editid;
                        QuesType.EditQuesType(quesTypeInfo);
                        Utility.ReloadTopPage(this);
                    }
                }
                else
                {
                    if (QuesType.CheckQuesTypeExistByName(QuesTypeName.Text.Trim(), QuesTypeKeyword.Text))
                    {
                        Utility.ShowJsAlert("该题型已经存在，或关键字重复", this);
                    }
                    else
                    {
                        QuesType.AddQuesType(quesTypeInfo);
                        Utility.ReloadTopPage(this);
                    }
                }
              
            }
            else
            {
                Utility.ShowJsAlert("难度级别名称和关键字不能为空", this);
            }
        }
    }
}
